#!/bin/sh

keybase ctl init

if ! keybase version --assert-matching >/dev/null 2>&1; then
    echo "restarting outdated services..."
    systemctl --user daemon-reload
    systemctl --user restart keybase kbfs
else
    systemctl --user start keybase kbfs
fi

if [ $# -gt 0 ] || systemctl --user is-active --quiet keybase.gui; then
    exec electron17 /usr/share/keybase-app "$@"
else
    systemctl --user import-environment KEYBASE_AUTOSTART
    exec systemctl start --user --wait keybase.gui
fi
